package fun.qu_an.minecraft.mini_games_helper;


import fun.qu_an.minecraft.mini_games_helper.command.MGHReloadCommand;
import fun.qu_an.minecraft.mini_games_helper.config.MiniGamesHelperConfig;
import fun.qu_an.minecraft.mini_games_helper.network.MGHConfigS2CPacket;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.networking.v1.*;
import net.minecraft.client.network.PlayerListEntry;

public class MiniGamesHelper implements ModInitializer {
	public static final String MOD_ID = "mini_games_helper";

	@Override
	public void onInitialize() {
		MiniGamesHelperConfig.load();

		CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
			MGHReloadCommand.register(dispatcher);
		});

//		ServerLoginConnectionEvents.QUERY_START.register((handler, server, sender, synchronizer)
//			-> MGHConfigS2CPacket.sendToClient(sender, MiniGamesHelperConfig.get()));
		ServerConfigurationConnectionEvents.CONFIGURE.register((handler, server)
			-> MGHConfigS2CPacket.sendToClient(handler, MiniGamesHelperConfig.get()));
		ServerLifecycleEvents.END_DATA_PACK_RELOAD.register((server, serverResourceManager, success)
			-> {
			MiniGamesHelperConfig.load();
			server.getPlayerManager().getPlayerList().forEach(player
				-> MGHConfigS2CPacket.sendToClient(player.networkHandler, MiniGamesHelperConfig.get()));
		});
	}
}
